//
//  YJCNetWork.m
//  xiangzhe
//
//  Created by lyt on 16/11/14.
//  Copyright © 2016年 com.dazhebao. All rights reserved.
//
#ifdef DEBUG
#define baseURL @"http://aapi.dazhebao.cc/"
#else
#define baseURL @"http://api.dazhebao.cc/"
#endif

#import "YJCNetWork.h"


@implementation YJCNetWork

+ (instancetype)sharedNetWork{
    
    static YJCNetWork *instance;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] initWithBaseURL:[NSURL URLWithString:baseURL]];
    });
    
    return instance;
    
}

- (instancetype)initWithBaseURL:(NSURL *)url {
    self = [super initWithBaseURL:url];
    
    if (self) {
        [self.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
        }];
        [self.reachabilityManager startMonitoring];
        
        self.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain",@"text/html", nil];
        [(AFJSONResponseSerializer *)self.responseSerializer setRemovesKeysWithNullValues:YES];
    }
    return self;
}
// 封装 AFN 网络请求
- (void)requestWithMethod:(YJCRequestMethod)method serviceName:(NSString *)serviceName parameters:(NSDictionary *)param finshed:(YJCRequestCallBack)finsh{
    // 公共可选参数
    NSDictionary *param1 = @{
                             @"versionCode":@"",//版本
                             @"versionName":@"",//版本名称
                             @"OS":@"",//操作系统
                             @"platForm":@"",//平台信息
                             @"mobileVersion":@"",//操作系统版本
                             @"mobileModel":@"",//手机型号
                             @"refreshToken":kRefreshToken
                             };
    // 将我传入的字典和公共的字典转成字符串  再赋值给最终的字典的一个 key 值
    NSMutableDictionary *param2 = [[NSMutableDictionary alloc] initWithDictionary:param1];
    [param2 addEntriesFromDictionary:param];
    NSLog(@"%@",param2);
    NSData *data = [NSJSONSerialization dataWithJSONObject:param2.copy options:0 error:nil];
    NSString *params = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSString *str = @"";
    NSString *token = @"12345";
    NSString *refreshToken = kRefreshToken;
    NSDictionary *parameters = @{
                                 @"service":serviceName,
                                 @"token":token,
                                 @"refreshToken":refreshToken,
                                 @"param":params
                                 };
    
    [self requestWithMethod:method URLString:str parameters:parameters finshed:^(id result, NSError *error) {
        finsh(result,error);
    }];
    
}

- (void)requestWithMethod:(YJCRequestMethod)method URLString:(NSString *)URLString parameters:(NSDictionary *)parameters finshed:(YJCRequestCallBack)finsh{
    
    if (method == GET) {
        [[YJCNetWork sharedNetWork] GET:URLString parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            finsh(responseObject,nil);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            finsh(nil,error);
        }];
    }else{
        [[YJCNetWork sharedNetWork] POST:URLString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            finsh(responseObject,nil);
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            finsh(nil,error);
        }];
        
    }
    
}
@end
